import { defineStore } from 'pinia'
import service from "@/utils/request"

const useDataMapStore = defineStore(
  'dataMap',
  {
    state: () => ({
      values: {}
    }),
    actions: {
      // 获取缓存数据
      getDataMap({ key, defaultValue = [] }) {
        if (!key) {
          return defaultValue;
        }
        return this.values[key] || defaultValue;
      },
      // 设置缓存数据
      async initDataMap(params = []) {
        for (let i = 0; i < params.length; i++) {
          const { key, api } = params[i]
          if (key) {
            let result = await service.get(api)
            if (result && result.data) {
              this.values[key] = result.data
            }
          }
        }
      }
    }
  })

export default useDataMapStore
